package com.iot.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iot.commonutils.utils.Result;
import com.iot.menu.entity.Menu;
import com.iot.menu.mapper.MenuMapper;
import com.iot.menu.service.MenuService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询所有菜单
     *
     * @return
     */
    @Override
    public Result getAllMenus() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        List<Menu> result = menuMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(result)) {
            for (Menu menu : result) {
                Menu parent = menuMapper.selectById(menu.getMenuPid());
                if (parent != null) {
                    menu.setMenuPid(parent.getMenuName());
                }
            }
        }
        return Result.success("菜单列表展示成功!", result);
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @Override
    public Result addMenus(Menu menu) {
        if (StringUtils.isEmpty(menu.getMenuName()) && StringUtils.isEmpty(menu.getMenuPid())
                && StringUtils.isEmpty(menu.getSort() + "") && StringUtils.isEmpty(menu.getMenuType() + "")) {
            return Result.error("请确保名称,所属菜单,排序码必须填写!");
        }
        //判断是否有重复的名字
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Menu> name = queryWrapper.eq("menu_name", menu.getMenuName());
        List<Menu> listName = list(name);
        if (listName.size() > 0 || listName != null) {
            Result.error("频道名字已经存在,请换个名字重试!");
        }
        menu.setCreateBy(redisTemplate.opsForValue().get("NAME"));
        menu.setCreateTime(new Date());
        boolean save = save(menu);
        if (save) {
            return Result.success("添加菜单成功!");
        }
        return Result.error("添加菜单失败!");
    }

    /**
     * 更新菜单
     *
     * @param menu
     * @return
     */
    @Override
    public Result updateMenu(Menu menu) {
        int result = menuMapper.updateById(menu);
        if (result != 1) {
            return Result.error("更新数据失败!");
        }
        return Result.success("更新数据成功!");
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteMenu(String id) {
        if (id == null) {
            return Result.error("id值为空,请重新输入!");
        }
        int result = menuMapper.deleteById(id);
        if (result == 1) {
            return Result.success("删除菜单成功!");
        }
        return Result.error("删除用户失败!");
    }
}
